import { TextEdit, Button, VerticalBox } from "std-widgets.slint";

struct Prompt {
    show: bool,
    is-success: bool,
    text: string,
}

export component MainWindow inherits Window {
    preferred-width: 314px;
    title: "二维码生成器v0.1.0";
    
    in property <image> qrcode;
    in-out property <string> content;
    out property <Prompt> prompt;
    
    callback generate;
    callback save;
    callback show-prompt(string, bool);

    show-prompt(s, b) => {
        prompt = {
            show: true,
            is-success: b,
            text: s,
        };
        timer.running = true;
    }

    timer := Timer {
        interval: 3s;
        running: false;
        triggered() => {
            prompt.show = false;
            self.running = false;
        }
    }
    
    TouchArea { clicked => { te.clear-focus() }}

    VerticalLayout {
        padding: 18px;
        padding-top: 10px;

        // 提示
        Rectangle {
            height: 12px;
            
            if prompt.show: Text {
                text: "- " + prompt.text + " -";
                font-size: 12px;
                color: prompt.is-success ? #00b300 : #ff1a1a;
            }
        }

        // 输入框
        VerticalBox {
            padding-top: 10px;

            te := TextEdit {
                text <=> content;
                placeholder-text: "在此输入内容";
                font-size: 13px;
                height: 80px;
            }
        }

        // 生成和保存按钮
        VerticalBox {
            padding-top: 18px;

            Button {
                enabled: ! content.is-empty;
                text: "生成二维码";
                clicked => { generate() }
            }

            Button {
                enabled: qrcode.width > 0;
                text: "保存至本地";
                clicked => { save() }
            }
        }

        // 二维码图像
        VerticalBox {
            height: 232px;

            Image { source: qrcode; }
        }
    }
}
